//
//  HSMessageModel.swift
//  home-school
//
//  Created by xlz on 16/7/6.
//  Copyright © 2016年 张晓林. All rights reserved.
//

import UIKit

/// 消息页面的数据模型，网络获得的数据字典需转换为模型对象
class HSMessageModel: NSObject {
    var not_Name: String?
    var not_content: String?
    var not_courseName: String?
    var not_createDate: String?
    var not_id: NSNumber?
    var not_sex: String?
    var not_tId: NSNumber?
    var not_title: String?
    var not_tpath: String?
    var not_type: NSNumber?
    
    var isShowCommentView:Bool = true   // 是否显示回复视图
    
    /// 回复视图的宽度
    let commentWidth: CGFloat = screenW - 50 - 30
    /// 回复视图高度
    var commentHeight: CGFloat = 0
    /// cell的高度
    var cellHeight: CGFloat = 0

    /// 点赞人数
    var likenum: NSNumber?
    /// 点赞人名
    var likelist: String? {
        didSet {
            // 当名称改变时从新计算高度
//            cellHeight = calculateCellHeight()
         }
    }
    
    /// 回复
    var replaylist: NSArray? {
        didSet {
            // 当值改变时重新计算高度
//            cellHeight = calculateCellHeight()
        }
    }
    
    /// 重载构造函数
    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeys(dict)
    }
    
    /// 计算cell行高
    func calculateCellHeight() -> CGFloat {
        // 获得第一条分割线以上的视图高度
        let H_one: CGFloat = 50 + 16 + 1 // 图片高度+间距*2 + 线高
        
        // 计算第一个标题的高度
        var titleH: CGFloat = 0
        if let title = not_title {
            titleH = (title as NSString).boundingRect(with: CGSize(width:commentWidth+8,height: 100), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: fontSizeTitle)], context: nil).size.height
        }
        
        // 计算消息内容高度
        var contentH: CGFloat = 0
        if let content = not_content {
            contentH = (content as NSString).boundingRect(with: CGSize(width: commentWidth+8,height: CGFloat(MAXFLOAT)), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: fontSizeContent)], context: nil).size.height
        }
        
        let H_tow: CGFloat = 4*8 + 11 + titleH + contentH // 3个间距+回复视图高+标题和内容的高
        
        // 以下计算回复视图的高度
        // 点赞人数内容高度
        var likeViewHeight: CGFloat = 0
        // 回复内容高度
        var replyViewHeight: CGFloat = 0

        // 计算当前点赞人名字符串所占的高度
        let likeN = likenum?.intValue ?? 0
        if likeN > 0 {
            // 拼接图片
            let attach = NSTextAttachment()
            attach.image = UIImage(named: "notifi_support_press")
            attach.bounds = CGRect(x: 0, y: -2, width: 12, height: 12)
            let imageAttribute = NSAttributedString(attachment: attach)
            let attributeStr = NSMutableAttributedString(attributedString: imageAttribute)
            let likeStr = " : \(likelist)"
            let likeAttribute = NSAttributedString(string: likeStr)
            attributeStr.append(likeAttribute)
            
            // 计算
            let textSize = attributeStr.boundingRect(with: CGSize(width: commentWidth,height: CGFloat(MAXFLOAT)), options: NSStringDrawingOptions.usesLineFragmentOrigin, context: nil).size
            
            likeViewHeight = textSize.height + 15
        }
        
        // 计算回复内容所占高度
        if let replylistNum = replaylist?.count {
            // 计录回复标签
            var replyH: CGFloat = 0
            for i in 0..<replylistNum {
                if replaylist![i] is HSReplaylistModel {
                    let reply = replaylist![i] as! HSReplaylistModel
                    let name = reply.int_name!
                    let content = reply.int_content!
                    // 拼接显示文字
                    let replyInfo = "\(name)：\(content)"
                    // 计算
                    let textSize = (replyInfo as NSString).boundingRect(with: CGSize(width: commentWidth,height: CGFloat(MAXFLOAT)), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: fontSizeCommentV)], context: nil).size
                    replyH += textSize.height
                }
            }
            replyViewHeight = replylistNum > 0 ? replyH + 5 : replyH
        }
        
        // 回复视图的高度
        if likeViewHeight == 0 {
            commentHeight = replyViewHeight + 8
        }else {
            commentHeight = likeViewHeight + replyViewHeight + 3
        }
        
        let H_four: CGFloat = 2*8 + 11 // 2个间距 + 底部视图高度
        
        // 计算总高度
        let H_total = H_one + H_tow + commentHeight + H_four
        
        return H_total // 返回总高度
    }
}


/*
 {
 likelist = "\U738b\U6d77\U5a77 \U5bb6\U957f";
 likenum = 1;
 "not_Name" = "\U738b\U6d77\U5a77 \U5bb6\U957f";
 "not_content" = content;
 "not_courseName" = "\U5bb6\U957f";
 "not_createDate" = "2016-07-07 20:12:10";
 "not_id" = 11;
 "not_sex" = "\U5973";
 "not_tId" = 2;
 "not_title" = "this is title";
 "not_tpath" = "";
 "not_type" = 2;
 replaylist =  (
     {
     "int_content" = "OK?";
     "int_name" = "\U738b\U6d77\U5a77 \U5bb6\U957f";
     "user_type" = 2;
     },
     {
     "int_content" = ok;
     "int_name" = "\U5f20\U6960 \U5bb6\U957f";
     "user_type" = 2;
     }
 );


 */






